/*
 * Copyright 2011 Stefan Ferstl
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.debmavenplugin;

import java.io.File;
import java.io.IOException;

import com.google.code.debmavenplugin.archiver.ArchiveMetaData;
import com.google.code.debmavenplugin.archiver.ar.deb.ControlArchiveArchiver;
import com.google.code.debmavenplugin.archiver.ar.deb.DebPackageArchiver;
import com.google.code.debmavenplugin.archiver.targz.TarGzArchiver;
import com.google.code.debmavenplugin.common.StringToFileWriter;


/**
 * <p>
 * Creates a Debian package.
 * </p>
 * <p>
 * To create the package, this class needs to know the package's content the
 * location of the control files and the directory where to store the created
 * Debian package.
 * </p>
 */
public class DebianPackager {
  private static final String DEBIAN_BINARY_FILE = "debian-binary";
  private static final String DATA_ARCHIVE_FILE = "data.tar.gz";


  private ArchiveMetaData dataArchiveMetaData;
  private final File controlFileDir;
  private final File outputDir;
  private final String packageName;

  /**
   * Constructor. Initializes all the required data for the Debian package to be
   * created.
   * @param dataArchiveMetaData Metadata describing the content of the Debian
   *        package.
   * @param controlFileDir Directory containing the control files (
   *        <code>control, postinst, prerm</code>, the <code>md5sums</code> file
   *        will be generated)
   * @param packageName Name of the Debian package to be generated.
   * @param outputDir Directory where the generated Debian package is stored.
   */
  public DebianPackager(ArchiveMetaData dataArchiveMetaData, File controlFileDir, String packageName, File outputDir) {
    this.dataArchiveMetaData = dataArchiveMetaData;
    this.controlFileDir = controlFileDir;
    this.packageName = packageName;
    this.outputDir = outputDir;
  }

  /**
   * Creates the debian package.
   * @throws IOException In case something goes wrong.
   */
  public void createDebianPackage() throws IOException {
    File dataArchive = new File(this.outputDir, DATA_ARCHIVE_FILE);
    TarGzArchiver dataArchivePacker = new TarGzArchiver(dataArchive, this.dataArchiveMetaData, true);
    try {
      dataArchivePacker.createArchive();
    } catch (IOException e) {
      throw new IOException("Error while creating data archive for debian package '" + this.packageName + "'.", e);
    }

    ControlArchiveArchiver controlArchivePacker = new ControlArchiveArchiver(this.dataArchiveMetaData, this.controlFileDir, this.outputDir);
    try {
      controlArchivePacker.createArchive();
    } catch (IOException e) {
      throw new IOException("Error while creating control archive for debian package '" + this.packageName + "'.", e);
    }

    StringToFileWriter debianBinaryWriter = new StringToFileWriter(new File(this.outputDir, DEBIAN_BINARY_FILE), "2.0\n", "ISO-8859-1");
    try {
      debianBinaryWriter.createFile();
    } catch (IOException e) {
      throw new IOException("Error while creating debian-binary file for debian package '" + this.packageName + "'.", e);
    }

    DebPackageArchiver debPacker = new DebPackageArchiver(this.outputDir, this.packageName);
    try {
      debPacker.createArchive();
    } catch (IOException e) {
      throw new IOException("Error while creating debian package '" + this.packageName + "'.", e);
    }
  }
}
